package responder;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DBComm.BuddyList;
import messages.ChatMessage;
import DBComm.DBComm;

public class WebResponder extends HttpServlet {
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		DBComm dbcomm = new DBComm();
		String type = req.getParameter("type");
		
		PrintWriter out = resp.getWriter();
		
		if (type.compareTo("addbuddy") == 0){
			String clientID = req.getParameter("clientID");
			String buddy = req.getParameter("buddy");
			String group = req.getParameter("group");
			int status = dbcomm.addBuddy(clientID, buddy, group);
			//out.println("addbuddy: " + clientID + ", " + buddy + ", " + group);
			out.println(status);
			
		} else if (type.compareTo("removebuddy") == 0){
			String clientID = req.getParameter("clientID");
			String buddy = req.getParameter("buddy");
			String group = req.getParameter("group");
			int status = dbcomm.removeBuddy(clientID, buddy, group);
			//out.println("removebuddy: " + clientID + ", " + buddy + ", " + group);
			out.println(status);
			
		} else if (type.compareTo("register") == 0){
			String clientID = req.getParameter("clientID");
			String password = req.getParameter("password");
			int status = dbcomm.registerUser(clientID, password);
			//out.println("register: " + clientID + ", " + password);
			out.println(status);
			
		} else if (type.compareTo("getbuddylist") == 0){
			String clientID = req.getParameter("clientID");
			BuddyList bl = dbcomm.getBuddyList(clientID);
			
			String bljson = JSONGenerator.buddyListGenerator(bl);
			resp.setContentType("text/javascript");
			out.println(bljson);
			
		} else if (type.compareTo("gethistory") == 0){
			String clientID = req.getParameter("clientID");
			dbcomm.getChatHistory(clientID);
			
		} else if (type.compareTo("login")==0){
			String loginID = req.getParameter("loginID");
			String password = req.getParameter("password");
			int result = dbcomm.verifyLogin(loginID, password);
			out.println(result);
			
		} else if (type.compareTo("sendmessage") == 0){
			String to = req.getParameter("to");
			String from = req.getParameter("from");
			String message = req.getParameter("message");
			
			int result = dbcomm.addMessage(from, message, to);
			out.println(result);
		}  else if (type.compareTo("time") == 0){
			    Calendar cal = Calendar.getInstance();
			    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
			    out.println(sdf.format(cal.getTime()));
			    
		} else if (type.compareTo("event") == 0){
			String clientid = req.getParameter("clientid");
			int count = 0;
			int eventCode;
			
			do {
				//check the database if there are any messages greater than lastid
				//if there is stuff in the DB, break out of this loop
				//otherwise, sleep, and check again!
				eventCode = dbcomm.getWebEvent(clientid);
				
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				count++;
			} while (count < 29 && eventCode < 1);
			
			if (eventCode == 1){
				//new messages
				//TODO:
				ArrayList<ChatMessage> messages = dbcomm.getMessage(clientid);
				String cm = JSONGenerator.messagesGenerator(messages);
				dbcomm.clearEvent(clientid);
				resp.setContentType("text/javascript");
				out.println(cm);
			} else if (eventCode == 2){
				BuddyList bl = dbcomm.getBuddyList(clientid);
				dbcomm.clearEvent(clientid);
				
				String bljson = JSONGenerator.buddyListGenerator(bl);
				resp.setContentType("text/javascript");
				out.println(bljson);
			} else if (eventCode == 3){
				//do both
				ArrayList<ChatMessage> messages = dbcomm.getMessage(clientid);
				BuddyList bl = dbcomm.getBuddyList(clientid);
				String stuff = JSONGenerator.messageAndStatus(messages, bl);
				resp.setContentType("text/javascript");
				out.println(stuff);
				dbcomm.clearEvent(clientid);
			} else {
				out.println(0);
			}
			
			//out.println("eventcode: " +eventCode);
		}
		
		dbcomm.closeConnection();
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		DBComm dbcomm = new DBComm();
		String type = req.getParameter("type");
		String clientid = req.getParameter("clientid");
		PrintWriter out = resp.getWriter();
		
		if (type.compareTo("status") == 0){
			int status = Integer.parseInt(req.getParameter("status"));
			dbcomm.updateStatus(clientid, status);
		} 
	}
}
